﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using Xiucai.Common;
using Xiucai.Common.Data.SqlServer;

namespace Xiucai.BPM.Admin.Sky.ashx
{
    /// <summary>
    /// Sky 的摘要说明
    /// </summary>
    public class Sky : IHttpHandler
    {
        private object obj = new object();
        string NameI, NameII, Food, FoodSum, People;
        decimal NameIM = 0, NameIIM = 0, FoodM = 0, Sum = 0;
        string time;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            switch (action)
            {
                case "InsertData":
                    GetRequestData(context);
                    var result = InsertData();
                    context.Response.Write(result.ToString());
                    break; ;
                case "DeleteData":
                    string Id = context.Request["Id"];
                    context.Response.Write(DeleteData(Id).ToString());
                    break;
                case "PayData":
                    string Id1 = context.Request["Id"];
                    context.Response.Write(PayData(Id1).ToString());
                    break;
                case "PayNoData":
                    string Id2 = context.Request["Id"];
                    context.Response.Write(PayNoData(Id2).ToString());
                    break;                    
                case "InsertMenuData":
                    string Menu = context.Request["Menu"];
                    string DateTime3 = context.Request["DateTime"];
                    context.Response.Write(InsertMenuData(Menu, DateTime3).ToString());
                    break;
                case "GetMenuData":
                    string DateTime1 = context.Request["DateTime"];
                    context.Response.Write(GetMenuData(DateTime1));
                    break;
                case "GetStatus":
                    string DateTime2 = context.Request["DateTime"];
                    context.Response.Write(GetStatus(DateTime2));
                    break;
                default:
                    string DateTime = context.Request["DateTime"];
                    var result1 = GetData(DateTime);
                    context.Response.Write(result1);
                    break;
            }
        }

        public int InsertData()
        {
            lock (obj)
            {
                StringBuilder isExit = new StringBuilder();
                StringBuilder sql = new StringBuilder();
                isExit.AppendFormat(@" select * from [dbo].[T_Reservation] where People='{0}' and Date='{1}'", People, time);
                DataSet ds = SqlEasy.ExecuteDataSet(isExit.ToString());
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                {
                    sql.AppendFormat(@" update [dbo].[T_Reservation] set VegetableI='{0}',VegetableIMoney='{1}', VegetableII='{2}',VegetableIIMoney='{3}',FoodMain='{4}',FoodMainCount='{5}',FoodMainMoney='{6}',Sum='{7}' where id='{8}'",
                        NameI, NameIM, NameII, NameIIM, Food, FoodSum, FoodM, Sum, ds.Tables[0].Rows[0]["Id"].ToString());
                }
                else
                {
                    sql.AppendFormat(@"INSERT [dbo].[T_Reservation] ([Date], [VegetableI], [VegetableII], [VegetableIMoney], [VegetableIIMoney], [FoodMain], [FoodMainCount], [FoodMainMoney], [Sum], [People],[Pay]) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}')",
                         time, NameI, NameII, NameIM, NameIIM, Food, FoodSum, FoodM, Sum, People, "否");
                }
                return SqlEasy.ExecuteNonQuery(sql.ToString());
            }
        }
        public int DeleteData(string id)
        {
            lock (obj)
            {
                StringBuilder sql = new StringBuilder();
                sql.Append(" delete  from [dbo].[T_Reservation] where Id='" + id + "'");
                return SqlEasy.ExecuteNonQuery(sql.ToString());
            }
        }

        public int PayData(string id)
        {
            lock (obj)
            {
                StringBuilder sql = new StringBuilder();
                sql.Append(" update [dbo].[T_Reservation] set pay='是' where Id='" + id + "'");
                return SqlEasy.ExecuteNonQuery(sql.ToString());
            }
        }
        public int PayNoData(string id)
        {
            lock (obj)
            {
                StringBuilder sql = new StringBuilder();
                sql.Append(" update [dbo].[T_Reservation] set pay='否' where Id='" + id + "'");
                return SqlEasy.ExecuteNonQuery(sql.ToString());
            }
        }
        public string GetData(string time)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from [dbo].[T_Reservation] where date='{0}' order by VegetableI,foodmain  ", time);
            DataSet ds = SqlEasy.ExecuteDataSet(sql.ToString());
            return JSONhelper.ToJson(ds);
        }

        public void GetRequestData(HttpContext context)
        {
            NameI = context.Request["NameI"] != null ? context.Request["NameI"].ToString() : "";
            decimal.TryParse(context.Request["NameIM"] != null ? context.Request["NameIM"].ToString() : "", out NameIM);
            NameII = context.Request["NameII"] != null ? context.Request["NameII"].ToString() : "";
            decimal.TryParse(context.Request["NameIIM"] != null ? context.Request["NameIIM"].ToString() : "", out NameIIM);
            Food = context.Request["Food"] != null ? context.Request["Food"].ToString() : "";
            FoodSum = context.Request["FoodSum"] != null ? context.Request["FoodSum"].ToString() : "";
            decimal.TryParse(context.Request["FoodM"] != null ? context.Request["FoodM"].ToString() : "", out FoodM);
            People = context.Request["People"] != null ? context.Request["People"].ToString() : "";
            time = context.Request["DateTime"];
            if (NameI != "")
                Sum += NameIM;
            if (NameII != "")
                Sum += NameIIM;
            if (Food != "")
                Sum += FoodM;
            if ((NameI != "" && NameIM != 0) && (NameII != "" && NameIIM != 0))
                Sum = Sum - 1;
        }
        public int InsertMenuData(string menu,string time)
        {
            lock (obj)
            {
                StringBuilder isExit = new StringBuilder();
                StringBuilder sql = new StringBuilder();
                isExit.AppendFormat(@" select * from [dbo].[T_Menu] where  Date='{0}'", time);
                DataSet ds = SqlEasy.ExecuteDataSet(isExit.ToString());
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                {
                    sql.AppendFormat(@" update [dbo].[T_Menu] set MenuContent='{0}' where id='{1}'", menu, ds.Tables[0].Rows[0]["Id"].ToString());
                }
                else
                {
                    sql.AppendFormat(@"INSERT [dbo].[T_Menu] ([Date],[MenuContent]) VALUES ('{0}','{1}')",
                         time, menu);
                }
                return SqlEasy.ExecuteNonQuery(sql.ToString());
            }
        }
        public string GetMenuData(string time)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from [dbo].[T_Menu] where date='{0}' ", time);
            DataSet ds = SqlEasy.ExecuteDataSet(sql.ToString());
            return JSONhelper.ToJson(ds);
        }
        public int GetStatus(string time)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from dbo.T_Reservation where Date='{0}' and Pay='是' ", time);
            DataSet ds = SqlEasy.ExecuteDataSet(sql.ToString());
            if (ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                return 1;
            else
                return 0;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}